package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class Delete implements Ioperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");

        int currentSize = bookList.getUsedSize();
        if(currentSize == 0) {
            System.out.println("书架为空，不能删除！");
            return;
        }

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除的图书:");
        String name = scanner.nextLine();



        int index = -1;
        //如果没有这本书 就不能删除了
        for (int i = 0; i < currentSize; i++) {
            Book book1 = bookList.getBook(i);
            if(book1.getName().equals(name)){
                index = i;
                break;
            }
        }
        if (index == -1){
            System.out.println("没有这本书，无法删除！");
        }
        for (int i = index; i < currentSize-1; i++) {
            Book book1 = bookList.getBook(i+1);
            bookList.setBook(i,book1);
            System.out.println("删除成功");
        }
        bookList.setUsedSize(currentSize-1);
    }
}
